{Hint: save all files to location: D:\Lamw\workspace\erp\jni }
unit LoginModule;

{$mode delphi}

interface

uses
  Classes, SysUtils, AndroidWidget, Laz_And_Controls, preferences, lNet, MainModule, fpjson, ucommon, ubase64;
  
type

  { TLoginForm }

  TLoginForm = class(jForm)
    jBtnCommit: jButton;
    jEdtPassword: jEditText;
    jEdtUsername: jEditText;
    jImageLst: jImageList;
    jImgLogo: jImageView;
    jPanVersion: jPanel;
    jPanLogo: jPanel;
    jPanTitle: jPanel;
    jPanUserInfo: jPanel;
    jPreferens: jPreferences;
    jTexTitle: jTextView;
    jTxtInfo: jTextView;
    jTxtver: jTextView;
    jTxtUserName: jTextView;
    jTxtPassWord: jTextView;
    jTimerLNet: jTimer;
    procedure jBtnCommitClick(Sender: TObject);
    procedure jEdtPasswordEnter(Sender: TObject);
    procedure jTimerLNetTimer(Sender: TObject);
    procedure LoginFormCreate(Sender: TObject);
    procedure LoginFormJNIPrompt(Sender: TObject);

    procedure OnLTcpReceived(aSocket: TLSocket);
    procedure OnLTcpError(const msg: string; aSocket: TLSocket);
  private
    {private declarations}
  public
    {public declarations}
    lConn: TLTCP;
  end;

var
  LoginForm: TLoginForm;

implementation

{$R *.lfm}
  

{ TLoginForm }

procedure TLoginForm.LoginFormCreate(Sender: TObject);
begin
  lConn:= TLTCP(gPtrConn^);
  lConn.OnReceive := OnLTcpReceived;
  lConn.OnError   := OnLTcpError;
  lConn.Disconnect(True);
end;

procedure TLoginForm.jBtnCommitClick(Sender: TObject);
var
  s, es: string;
  b : pbyte;
  i, n: dword;
  jsonObj, jsParam: TJSONObject;
begin
  jPreferens.SetStringData('USERNAME', jEdtUsername.Text);
  jPreferens.SetStringData('PASSWORD', jEdtPassword.Text);
  //ip := GetHostIP('link.taiperng.com');
  if lConn.Connect('120.237.73.54', 12300) then// 如果正常连接taiperng.vicp.net   120.237.73.54
  begin
    lConn.CallAction; //等待看是否连接
  end;
  // 准备参数提交
  jsonObj := TJSONObject.Create;
  jsParam := TJSONObject.Create;
  jsonObj.Add('TIME',Now());
  jsonObj.Add('CMD','GetUserInfo');
  jsonObj.Add('PARM', jsParam);
  jsParam.Add('PMA',jEdtUsername.Text);
  jsParam.Add('PMB',jEdtPassword.Text);
  // 加密
  s := jsonObj.AsJSON;
  es:= TFun.Encrypt(s);
  // BASE64
  i := length(es);
  n := (i div 3) * 4 + 5;
  Getmemory(b, n);
  FillByte(b^, n, 0);
  Base64Encode(PChar(es), i, b, n);
  //
  if lConn.connected then
    lConn.Send(b^,n);
  Freemem(b);
end;

procedure TLoginForm.jEdtPasswordEnter(Sender: TObject);
begin
  //
end;

procedure TLoginForm.jTimerLNetTimer(Sender: TObject);
begin
  if lConn.connected then lConn.CallAction;   //回调信息
end;

procedure TLoginForm.LoginFormJNIPrompt(Sender: TObject);
begin
  jEdtUsername.Text  := jPreferens.GetStringData('USERNAME','');
  jEdtPassword.Text  := jPreferens.GetStringData('PASSWORD','');
  jTimerLNet.Enabled := True;
end;


procedure TLoginForm.OnLTcpReceived(aSocket: TLSocket);
var
  s: string;
begin
  //接收信息
  lConn.GetMessage(s);
  jTxtInfo.Text := s;

  if MainForm = nil then
  begin
    gApp.CreateForm(TMainForm, MainForm);
    MainForm.TryBackTrackOnClose:= True;
    MainForm.Init(gApp);
  end else
  begin
     MainForm.Show;
  end;
end;

procedure TLoginForm.OnLTcpError(const msg: string; aSocket: TLSocket);
begin
  ShowMessage(msg);
end;

end.
